package io.renren.modules.common.service.impl;

import io.renren.modules.common.dao.TradeClassifyDao;
import io.renren.modules.common.dao.TradeJobDao;
import io.renren.modules.common.entity.TradeClassifyEntity;
import io.renren.modules.common.entity.TradeJobEntity;
import io.renren.modules.common.viewdo.TreeOption;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import io.renren.modules.common.dao.TradeCategoryDao;
import io.renren.modules.common.entity.TradeCategoryEntity;
import io.renren.modules.common.service.TradeCategoryService;


@Service("tradeCategoryService")
public class TradeCategoryServiceImpl extends ServiceImpl<TradeCategoryDao, TradeCategoryEntity> implements TradeCategoryService {

    @Autowired
    private TradeCategoryDao tradeCategoryDao;

    @Autowired
    private TradeClassifyDao tradeClassifyDao;

    @Autowired
    private TradeJobDao tradeJobDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<TradeCategoryEntity> page = this.page(
                new Query<TradeCategoryEntity>().getPage(params),
                new QueryWrapper<TradeCategoryEntity>()
        );

        return new PageUtils(page);
    }
    /*行业二级分类展开*/
    @Override
    public List<TreeOption> treeOptionCCJ(){

        List<TreeOption> topList = new ArrayList<>();

        List<TradeCategoryEntity> tradeCategoryEntities = tradeCategoryDao.selectList(null);
        List<TradeClassifyEntity> tradeClassifyEntities = tradeClassifyDao.selectList(null);
        List<TradeJobEntity> tradeJobEntities = tradeJobDao.selectList(null);

        topList = tradeCategoryEntities.stream().map(item -> new TreeOption(item.getTradeId(),item.getTradeName())).collect(Collectors.toList());

        for (TreeOption treeOption:topList){
            List<TradeClassifyEntity> collect = tradeClassifyEntities.stream().filter(item -> treeOption.getValue().equals(item.getTradeId())).collect(Collectors.toList());
            List<TreeOption> options = collect.stream().map(item -> new TreeOption(item.getClassifyId(), item.getClassifyName())).collect(Collectors.toList());
            treeOption.setChildren(options);
        }

/*        for (TreeOption treeOptionTop:topList){
            List<TreeOption> childrenTop = treeOptionTop.getChildren();
            for (TreeOption treeOptionMid : childrenTop){
                List<TradeJobEntity> collect = tradeJobEntities.stream().filter(item -> treeOptionMid.getValue().equals(item.getClassifyId())).collect(Collectors.toList());
                List<TreeOption> options = collect.stream().map(item -> new TreeOption(item.getJobTypeId(), item.getJobName())).collect(Collectors.toList());
                treeOptionMid.setChildren(options);
            }
        }*/
        return topList;
    }
}